package com.sms.web.system.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sms.web.rest.RestObject;
import com.sms.web.system.entity.SysRole;
import com.sms.web.system.service.SysMenuService;
import com.sms.web.system.service.SysRoleService;
import com.sms.web.system.service.SysUserService;

/**
 * @desc 角色控制器
 * @author WuJiangWu
 * @date 2018年7月25日上午00:13:00
 */
@RestController
@RequestMapping(value= "/admin/role")
public class RoleController {
    
    protected static Logger logger = LoggerFactory.getLogger(RoleController.class);
    
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysMenuService menuService;
    
    /**
     * 获取角色列表
     * @param id
     * @return
     */
    @GetMapping("/list")
    public RestObject getList(String queryLike, 
    		@RequestParam(defaultValue = "1") int pageNumber, 
    		@RequestParam(defaultValue = "20")int pageSize){
    	
    	RestObject rest = new RestObject();
    	Page<SysRole> page = roleService.findByKeyword(queryLike, pageNumber, pageSize);
    	rest.setState(0);
    	rest.setData(page);
    	return rest;
    }
    
    /**
     * 获取角色信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public RestObject getUser(@PathVariable() String id) {
    	
    	RestObject rest = new RestObject();
    	SysRole role = roleService.findById(id);
    	rest.setState(0);
    	rest.setData(role);
    	return rest;
    }
    
    /**
     * 添加/更新角色信息
     * @param user
     * @return
     */
    @PutMapping("/")
    public RestObject saveUser(SysRole role){
    	try {
    		SysRole newRole = roleService.saveRole(role);
            return RestObject.newOk(newRole);
        } catch (Exception e) {
            logger.error("保存角色信息时异常", e);
            return RestObject.newError(e.getMessage());
        }
    }
    
    @DeleteMapping("/{id}")
    public RestObject deleteUser(@PathVariable()String id){
    	try {
    		roleService.delete(id);
            return RestObject.newOk("success");
        } catch (Exception ex) {
            logger.error("删除菜单时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
    }
    //==========================角色菜单、角色用户 controller================================
    /**
     * 保存角色菜单
     * @param roleId
     * @param ids
     * @return
     */
    @PutMapping("/saveRoleMenu")
    public RestObject saveRoleMenu(@RequestParam()String roleId, @RequestParam()String ids){
    	roleService.saveRoleMenu(roleId, ids);
    	return RestObject.newOk();
    }
    
}
